define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function data() {
                return {
                    mounted: 0,
                    publish_text: "确认发布",
                    is_publish: 0,
                    placeholder: "这里写下....",
                    title: "",
                    groupData: {},
                    type: "events",
                    community_id:0,
                    topic_id:0,
                    disablePublish: "disable",
                    imgsString:"[]",
                };
            },
            el: "#pageAdd",
            created: function created() {
                this.pid = this.$route.params.pid;
                if(this.$route.params.community_id){
                    this.community_id = this.$route.params.community_id;
                }
                 if(this.$route.params.topic_id){
                    this.topic_id = this.$route.params.topic_id;
                }
                var user_info = app.getUserInfo();
                this.user_info = user_info ? user_info : {};
                this.loadGroupData();
                if(this.topic_id>0){
                     this.getTopic();
                }
               
            },
            mounted: function mounted() {
                if (this.mounted == 1) {
                    return;
                }
                app.upload("#pageAddUpload");
                this.mounted = 1;
                //app.setTitle(this.);
                // app.get()
                var _this = this;
                //监听上传图片
                $("#pageAddUpload").on("BoxUpload", function() {
                    // console.log("BoxUpload");
                    _this.checkPublish();
                });

            },
            methods: {
                checkPublish: function checkPublish(e) {
                    var imglength = $("#pageAdd").find(".photoList-content li").length;
                    if (this.title || imglength > 1) {
                        this.disablePublish = "";
                    } else {
                        this.disablePublish = "disable";
                    }
                },
                loadGroupData: function loadGroupData() {
                    var _this = this;
                    app.post("sns/group/find", {
                        id: this.pid
                    }, function(res, textStatus, xhr) {
                        //var data= JSON.parse(res);
                        if (res.code != 200) {
                            app.alert(res.msg);
                        } else {
                         
                            _this.groupData = res.data;
                            _this.type = res.data.type;
                           var contentConfig=_this.groupData.content;

                            _this.placeholder = contentConfig.issueContent;
                            _this.publish_text = contentConfig.issueBntText;
                            //console.log(_this.groupData.content.issuePageTitle);
                            app.setTitle(contentConfig.issuePageTitle);
                        }
                    });
                    console.log(this);
                },

                getTopic: function () {
                
                var that = this;
                 
                var topic_id = this.topic_id;

                
                app.get("sns/topic/detail", {
                  id: topic_id,
                }, function (res) {
                  if (res.code != 200) {
                    app.alert(res.msg);
                    return;
                  }
                  // that.getPidArray();
                  that.setData({ topicData: res.data, pid: res.data.pid,community_id: res.data.shop_id, type: res.data.type,title:res.data.title });
                  // 图片
                   if(res.data.pics.length>0){
                     var imgs =[];
                     for (var i  in res.data.pics) {
                         imgs.push({name:'',file_url:res.data.pics[i]});
                     }
                      that.imgsString = JSON.stringify(imgs);
                        that.$nextTick(function(){
                         app.upload($("#pageAddUpload"));
                      });
                   }
                  
                   // 图片完

                 }); 
                  
              },
              setData:function(data){
                     for(var i in data){
                        this[i]=data[i];
                     }
                },
                publish: function publish() {
                    var _this = this;
                    // var liLength = $("#pageAdd").find(".photoList-content li").length;
                    // var imglength = $("#pageAdd").find(".photoList-content li .imgList-file_url").length;
                    // var limit = liLength - imglength - 1;
                    if (this.title === "") {
                        app.alert("请添加描述");
                        return false;
                    }
                    
                    if (_this.is_publish === 1) {
                        return;
                    }
                      _this.publishYes();
                    // if (limit > 0) {
                    //     app.confirm({
                    //         text: "还有" + limit + "张图片正在上传中，是否确认提交？",
                    //         yes: function yes() {
                    //             _this.publishYes();
                    //         }
                    //     });
                    // } else {
                    //     _this.publishYes();
                    // }
                },
                publishYes: function publishYes() {
                    var _this = this;
                    if (_this.is_publish === 1) {
                        return;
                    }
                    var imageKeys =[];
                    $(".photoList-content li").each(function(i,li){
                       $(li).attr('url') &&  imageKeys.push($(li).attr('url'));
                    });
                    _this.publish_text = "发布中....";
                    _this.is_publish = 1;
                    var data = $("#pageAdd").find("form").serializeArray();
                    data.push({
                        name:'pics',
                        value: imageKeys.join(","),
                    });
                    console.log(data);
                     
                    app.post("sns/topic/add", data, function(res, textStatus, xhr) {
                        //var data= JSON.parse(res);
                        if (res.code != 200) {
                            app.alert(res.msg);
                            _this.is_publish = 0;
                        } else {
                            //console.log(res);
                            // console.log(typeof(pageIndex));
                            var pageIndexMv = app.getVm("index");
                            // if (typeof(pageIndexMv) != "undefined") {
                            if (pageIndexMv && pageIndexMv.items) {
                                var list = [ res.msg ];
                                pageIndexMv.items = list.concat(pageIndexMv.items);
                            }
                            app.alert("恭喜你，发布成功", function() {
                                _this.$router.go(-1);
                                // _this.$router.push({
                                //     name: "user/topic",
                                //     params: {
                                //         pid: _this.pid,
                                //         user_id: _this.user_info.user_id
                                //     }
                                // });
                            });
                        }
                        //_this.is_publish=0;
                        _this.publish_text = "提交成功";
                    });
                }
            }
        }).$mount("#app");
    };
    module.exports = controller;
});
